package com.dfrz.ch07.stuTast;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;

/**
 * 作者：wangmaokang
 * 日期: 2021/4/15 9:35
 * 描述:
 */
public class CalenderTest {
    public static void main(String[] args) {
        Calendar calendar=Calendar.getInstance();
        Date date=new Date();

//        System.out.println(calendar.get(Calendar.MONTH));
//        calendar.set(Calendar.DAY_OF_MONTH,1);
          calendar.add(Calendar.DAY_OF_YEAR,1);
        System.out.println("前一天:"+calendar.get(Calendar.YEAR)+"-"+(calendar.get(Calendar.MONTH)+1)+"-"+(calendar.get(Calendar.DAY_OF_MONTH)));
        System.out.println("后一天"+calendar.get(Calendar.YEAR)+"-"+(calendar.get(Calendar.MONTH)+1)+"-"+(calendar.get(Calendar.DAY_OF_MONTH)));
//        上周日
        int ri=calendar.get(Calendar.DAY_OF_MONTH);
        int week=calendar.get(Calendar.DAY_OF_WEEK)-1;
//        System.out.println(week);
        getLastSunday();
        getLastDayofLastMonth();
        getOneDayOfLastMonth();
        getToday();
        getTodayOfThisMonth();

    }
    //相差天数
    public static void dateDifference() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = null;
        Date date2 = null;
        try {
            date1 = sdf.parse("2017-11-06");
            date2 = sdf.parse("2018-12-06");
        } catch (ParseException e) {
            e.printStackTrace();
        }

        long date = (date2.getTime()-date1.getTime())/86400000;//一天86400000毫秒
        System.out.println(sdf.format(date1)+"和"+sdf.format(date2)+"相差"+date+"天");
    }
    //    4）获取上周周日的日期
    public static void getLastSunday() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        // 减去一个星期
        c.add(Calendar.WEEK_OF_MONTH,-1);
        //上周星期的今天是第几天,星期天是1 所以要减去1
        int d = c.get(Calendar.DAY_OF_WEEK)-1;
        //添加余下的天数
        c.add(Calendar.DAY_OF_WEEK,7-d);
        System.out.println(sdf.format(c.getTime()));

    }
    //    获取当天时间
    public static void getToday() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Calendar c = Calendar.getInstance();
        System.out.println(sdf.format(c.getTime()));
    }

    public static void getTodayOfThisMonth() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        //设置本月的日期为1
        c.set(Calendar.DAY_OF_MONTH,1);
        System.out.println(sdf.format(c.getTime()));}

    public static void getOneDayOfLastMonth() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        //设置本月的日期为1
        c.add(Calendar.MONTH,-1);
        c.set(Calendar.DAY_OF_MONTH,1);
        System.out.println(sdf.format(c.getTime()));
    }

    public static void getLastDayofLastMonth() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.set(Calendar.DAY_OF_MONTH,1);
        c.add(Calendar.DAY_OF_YEAR,-1);
        System.out.println(sdf.format(c.getTime()));
    }

    public static void Datetosecond() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = null;
        Scanner s = new Scanner(System.in);
        System.out.println("请输入一个时间(yyyy-MM-dd)");
        String str = s.nextLine();
        System.out.println(date1.getTime());

    }
    //12）long 转日期(年-月-日 时-分-秒)
    public static void longToDate(long a) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh-mm-ss");
        Date date = new Date(a);
        System.out.println(sdf.format(date));
    }
}
